package com;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.filter.CharacterEncodingFilter;

@SpringBootApplication
@EnableTransactionManagement
@MapperScan(basePackages = "com.link.**.mapper")
@ServletComponentScan
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
//@EnableDistributedTransaction
public class BaseApplication {

    //注入文件上传servlet，定义文件上传映射路径
    @Bean
    public ServletRegistrationBean uploadImgServlet() {
        //用ServletRegistrationBean包装servlet
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setLoadOnStartup(3);
        //指定urlmapping
        registrationBean.addUrlMappings("/upload");
        //指定name，如果不指定默认为dispatcherServlet
        registrationBean.setName("UploadImgServlet");
        return registrationBean;
    }

    /**
     * 配置Filter，用来处理字符编码
     */

    @Bean
    @Order(9)
    public FilterRegistrationBean encodingFilter() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        filter.setBeanName("encodingFilter");
        FilterRegistrationBean registrationFilter = new FilterRegistrationBean();
        registrationFilter.setFilter(filter);
        registrationFilter.setAsyncSupported(true);
        registrationFilter.setName("encodingFilter");
        registrationFilter.addUrlPatterns("/*");
        return registrationFilter;
    }


    /**
     * 启动入口方法
     */
    public static void main(String[] args) {
        SpringApplication.run(CoreApplication.class, args);
    }

}
